<template>
  <div>
    <page-dialog
      title="入库单导入"
      v-model:visible="shows"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <!-- 条件栏 -->
      <page-filter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      >
        <template v-slot:filter-selectType>
          <el-select
            v-model="formData.value"
            placeholder="请选择入库单来源"
            @change="selectTypeChange"
            :clearable="false"
          >
            <el-option
              v-for="item in selectTypeOption"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </template>
      </page-filter>
      <page-table
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :listen-height="false"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :highlight-row="true"
        :check-box="false"
        :isDynamic="true"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-storageUserName="scope">
          <span v-if="formData.value === '1'">{{
            scope.row.storeUserName
          }}</span>
          <span v-if="formData.value === '2'">{{
            scope.row.storageUserName
          }}</span>
        </template>
        <template v-slot:col-storeTime="scope">
          <span v-if="formData.value === '1'">{{ scope.row.storeTime }}</span>
          <span v-if="formData.value === '2'">{{ scope.row.storageTime }}</span>
        </template>
      </page-table>
    </page-dialog>
  </div>
</template>
<script setup name="addStorageOrder">
import PageFilter from '@/components/PageFilter/index'
import PageTable from '@/components/PageTable/index'
import PageDialog from '@/components/PageDialog/index'
import { getOtherStoreOrderPage, getReceivingOrderPage } from '@/api/stock.js'

const { proxy } = getCurrentInstance()
const emit = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
})

const selectRow = ref('')
const formData = reactive({
  value: '1',
})
const selectTypeOption = ref([
  { name: '其他入库', id: '1' },
  { name: '采购入库', id: '2' },
])
// 过滤相关配置
const filterInfo = reactive({
  query: {
    ...props.query,
    sort: 'store_time desc',
  },
  list: [
    { type: 'slot', value: 'selectType' },
    {
      type: 'input',
      label: '入库单号',
      value: 'orderNoLike',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  api: getOtherStoreOrderPage,
  data: [],
  fieldList: [
    { label: '入库单号', value: 'orderNo' },
    { label: '入库人', value: 'storageUserName', type: 'slot' },
    { label: '入库时间', value: 'storeTime', type: 'slot' },
  ],
})
const dialogInfo = reactive({
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: true,
      event: save,
      show: true,
    },
  ],
})

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emit('update:showDialog', newValue) // 更新父组件shows
  },
})

onMounted(() => {
  proxy.$nextTick(() => {
    getList()
  })
})
function getList() {
  tableInfo.refresh = Math.random()
}
function search() {
  // 重置分页
  tableInfo.initCurpage = Math.random()
  tableInfo.refresh = Math.random()
}
function selectTypeChange() {
  if (formData.value === '1') {
    tableInfo.api = getOtherStoreOrderPage
    filterInfo.query.sort = 'store_time desc'
    filterInfo.list[1].value = 'orderNoLike'
    delete filterInfo.query.storageUserNotNull
  } else if (formData.value === '2') {
    tableInfo.api = getReceivingOrderPage
    filterInfo.query.sort = 'storage_time desc'
    filterInfo.list[1].value = 'purchOrderNoLike'
    filterInfo.query.storageUserNotNull = 1
  }
  getList()
}
function save() {
  if (selectRow.value && selectRow.value.id) {
    handleClose()
    emit('callback', {
      id: selectRow.value.id,
      type: formData.value,
    })
  } else {
    proxy.$message({
      message: '请选择入库单',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
  }
}
function handleClose() {
  shows.value = false
  emit('reset')
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      dialogInfo.btList[1].disabled = !selectRow.value
      break
    case 'tableCheck':
      if (data.length) {
        selectRow.value = data
        dialogInfo.btList[1].disabled = !selectRow.value
      }
      break
  }
}
</script>
<style scoped lang="scss">
:deep(.el-dialog__body) {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
